import { Component, OnInit } from '@angular/core'
import { AcquirerNewBaseClass } from '../baseClass'
import { FormBuilder, Validators } from '@angular/forms'
import { AcquirerStoreService } from '../acquirer-store.service'
import { AddMinusService } from '@shared/services/addMinus.service'
import { IFormItem } from '@shared/interfaces/common'
import { makeAllControlDirty } from 'app/utils/helpers'
import { NzMessageService } from 'ng-zorro-antd'
import { I18NService } from '@core'

@Component({
  selector: 'app-acquirer-new-settlement',
  templateUrl: './settlement.component.html',
  providers: [
    AddMinusService,
  ]
})
export class AcquirerNewSettlementComponent extends AcquirerNewBaseClass implements OnInit {

  /* 是否提供报表 */
  ifGiveReport = false

  /* 可供选择的报表 */
  reportTypeOptions = [
    {
      label: 'TD01',
      value: 'TD01',
    },
    {
      label: 'AC01',
      value: 'AC01',
    },
    {
      label: 'AE01',
      value: 'AE01',
    },
    {
      label: 'EP01',
      value: 'EP01',
    },
    {
      label: 'DP01',
      value: 'DP01',
    }
  ]

  // 二期 TODO
  formItems: IFormItem[] = []

  constructor(
    public fb: FormBuilder,
    public msg: NzMessageService,
    public acquirerStore: AcquirerStoreService,
    public addMinusService: AddMinusService,
    private i18n: I18NService,
  ) {
    super(fb, acquirerStore)
  }

  ngOnInit() {
    this.initAddMinus()
    this.initValidateForm()
  }

  initAddMinus(): void {

  }

  handleIfGiveReportChange(ifGive: boolean) {
    if (!ifGive) {
      this.validateForm.value.reportList = []
    }
  }

  initValidateForm(): void {
    const { settleInfo } = this.acquirer.institution.common
    if (settleInfo.reportList.length > 0) {
      this.ifGiveReport = true
    }

    this.validateForm = this.fb.group({
      reportList: [settleInfo.reportList, [Validators.required]],
    })
  }

  checkValid(): boolean {
    if (!this.ifGiveReport) {
      return true
    }
    const valid = this.validateForm.valid
    if (valid) {
      this.pushDataToStore()
      this.msg.success(this.trans('GENERAL.NOTIFICATION.VALIDATION_SUCCESS'))
    } else {
      makeAllControlDirty(this.validateForm)
      this.msg.error(this.trans('GENERAL.NOTIFICATION.VALIDATION_FAIL'))
    }
    return valid
  }

  pushDataToStore(): void {
    this.acquirer.institution.common.settleInfo.reportList = this.validateForm.value.reportList
    this.acquirerStore.updateValue(this.acquirer)
  }

  trans(key: string, params?: Object): string {
    return this.i18n.fanyi(key, params)
  }
}
